var comments = Class.create();
Object.extend(comments, {
	/**
	 * 范占鳌添加 开始-----------
	 */
	/**
	 * 添加评论回复
	 * @param form_id
	 * @param editor_id
	 * @returns
	 */
	add_recomment: function(form_id, editor_id){
	$(form_id).content.value = editor_id.getHtml();
	var check = this.submit_check(form_id); //alert(check);   
	if(!check){ return false;}
	var form_obj=$(form_id);
	var pars=form_obj.serialize();
	form_obj.disable();
	var msg = '';
	var d = new Date();
	var url ='/~comment.index.reply_comment';
	var myAjax = new Ajax.Request(url,{
			method : 'post',
			parameters : pars,
			asynchronous: true,
			onLoading : function (event){
				curtime = d.getMilliseconds();
				msg = new MessageBox(MessageBox.TYPE_MESSAGE, '正在处理中，请稍后....', '提示','');
			},
			onComplete : function (transport){
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					if(d.getMilliseconds()-curtime>=1000){
						msg.close();
					}
					window.location.reload();
				}
				//this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id']);					
			}//.bindAsEventListener(this)
		});
	return false;
},
/**
 * 删除回复
 */
	del_recomment:function(thread_id,post_id){
	var url ='/~comment.index.delete_comment_post';
	var onComplete = function(transport){
			var ret = transport.responseText.evalJSON();
			if(ret.result){
				new Deletion('row_'+thread_id+'_'+post_id);
				//new MessageBox(MessageBox.TYPE_MESSAGE, '删除回复成功！', '提示','');
			}
		};
	var myAjax = function(){
			new Ajax.Request(
			url,{
			method : 'post',
			parameters :$H({post_id:post_id,thread_id:thread_id}),
			asynchronous: true,
			onComplete : onComplete
			}
		)};
	new MessageBox(MessageBox.TYPE_OK_CANCEL, '您确定要删除本条回复吗？', '', {ok: myAjax});
	},
	del_comment:function(thread_id){
	var url = "/~comment.index.delete_comment";
	var onComplete = function(transport){
			var ret = transport.responseText.evalJSON();
			if(ret.result){
				new Deletion('row_'+thread_id);
				//new MessageBox(MessageBox.TYPE_MESSAGE, '删除评论成功！', '提示','');
			}
		};
	var myAjax = function(){
			new Ajax.Request(
			url,{
			method : 'post',
			parameters :'thread_id='+thread_id, 
			asynchronous: true,
			onComplete : onComplete
			}
		)};
	new MessageBox(MessageBox.TYPE_OK_CANCEL, '您确定要删除本条评论吗？', '', {ok: myAjax});
	},
	submit_check: function(form_id){
		var form_obj = $(form_id);
		var conent_str = this.comment_trim(form_obj.content.value).replace(/<.*?>/g,""); 
		form_obj.content.value =  conent_str; 
		if(this.comment_trim(form_obj.content.value).length == 0  ){
			new MessageBox(MessageBox.TYPE_MESSAGE, '您的回复字数太少！', '提示','');
			return false;
		}
		if(this.comment_trim(form_obj.content.value).length >1000) {
			new MessageBox(MessageBox.TYPE_MESSAGE, '您的回复超出了字数限制', '提示','');
			return false;
		}else  return true;
	},
	
	/**
	 * 范占鳌添加 结束-----------
	 */
	app_comment:function(app,control,act,id,res_uid,code){
		var request_url = '/~comment.index.view_comment';		
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters : $H({app:app,control:control,act:act,id:id,res_uid:res_uid,code:code}),
				asynchronous: true,
				onComplete : function (httpReq){
					//adjust_content_frame();
					var comment_id = app+'_'+control+'_'+act+'_'+id;
					if ($(comment_id)) $(comment_id).innerHTML = '';
					var flag = httpReq.responseText.split('[:;:]');
					if ( flag[0] == 'comment_tip' ) {
							$(comment_id).insert(flag[1]);
					} else if (flag[0] == 'signature_tip') {
							new MessageBox(MessageBox.TYPE_MESSAGE,flag[1], '提示',{close:function(){}});
					} else {
						$(comment_id).insert(httpReq.responseText);
					}
					window.setTimeout(adjust_content_frame,50);
				}
			});
	},
	add_state: function(form_id){
		var check = this.state_submit_check(form_id);
		if(check){
			this.add_state_ajax(form_id);
			$(form_id).disable();
			return false;
		}
	},
	add_state_ajax:function(form_id){
		var check = this.state_submit_check(form_id);
		var form_obj=$(form_id);
		var pars=form_obj.serialize();
		var request_url ='/~comment.signature.add_state';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters : pars,
				onComplete : function (transport){
					//window.location.reload();
					window.location.href='~comment.signature.index';
					//setInterval(this.refush('~comment.signature.index'),500);
				}.bindAsEventListener(this)
			});
		form_obj.enable();		
	},
	refush_page: function(url){
		window.location.href=url;
	},
	state_submit_check:function(form_id){
		var form_obj = $(form_id);
		
		if($('state').value == '请填写您的心情!') { return false; }
		
		if($('state').value == '我正在做什么。。。') { return false; }
		
		if(($('state').value == '')&&($('smile_tage').value == ''))   { return false; }
		
		if(form_obj.state.value.length>100){   return false;
		}else if(form_obj.state.value.length <= 0){ 
			return false;
		}else{
			return true;
		}
	},
	remove_comment: function(thread_id){
		if(!confirm('你确认要删除这条评论和它的所有回复吗？')){   return false; }
		var request_url ='/~comment.index.delete_comment';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post', 
				parameters :'thread_id='+thread_id, 
				asynchronous: true,
				onComplete : function (transport){				
						var ret = transport.responseText.evalJSON();			
						this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id'],ret['data']['uid']);				
				}.bindAsEventListener(this)
			});
	},
	remove_comment_post: function(thread_id,post_id){
		if(!confirm('您确认要删除此回复吗？')){   return false; } //alert(post_id);
		var request_url ='/~comment.index.delete_comment_post';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters :$H({post_id:post_id,thread_id:thread_id}),
				asynchronous: true,
				onComplete : function (transport){						
						var ret = transport.responseText.evalJSON();				
						this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id'],ret['data']['uid']);					
					
				}.bindAsEventListener(this)
			});
	},
	remove_comment_list: function(thread_id){
		if(!confirm('您确认要删除此评论吗？')){   return false; }
		var request_url ='/~comment.index.delete_comment';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters :'thread_id='+thread_id,
				asynchronous: true,
				onComplete : function (){	window.location.reload();	}
			});
	},
	reply_comment:function(form_id, editor_id){
		$(form_id).content.value = editor_id.getHtml();	
		var check = this.comment_submit_check(form_id); if(!check){ return false;}
		var form_obj = $(form_id);
		var pars = form_obj.serialize();
		form_obj.disable();
		var request_url = '/~comment.index.reply_comment';
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters :pars,
				asynchronous: true,
				onComplete : function (transport){
						var ret = transport.responseText.evalJSON();
						this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id'],ret['data']['uid']);
				}.bindAsEventListener(this)
			});
	},
	reply_comment_list:function(form_id){
		var check = this.comment_submit_check(form_id); if(!check){ return false;}
		var form_obj = $(form_id);
		var pars = form_obj.serialize();
		var request_url = '/~comment.index.reply_comment';
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters :pars,
				asynchronous: true,
				onComplete : function (){  window.location.reload(); }
			});
	},
	comment_submit_check: function(form_id){
		var form_obj = $(form_id);
		var conent_str;
		var conent_str1;
		conent_str1 = (form_obj.content.value).replace(/<br>/ig,    "");
		conent_str1 = conent_str1.replace(/&nbsp;/g,    "");
		//alert(this.comment_trim(conent_str1).length);
		//alert(conent_str1);
		if(this.comment_trim(conent_str1).length == 0  ){
				$(form_id+'_msg').innerHTML = '<div class="warning-tip tip-box warning-tip-box"><div><span class="fixie6png tip-icon warning-tip-icon" ></span>您说什么？</div></div>';
				return 0;
		}

		conent_str = (form_obj.content.value).replace(/<br\s*\/?> /ig,    "\n ");
		conent_str = conent_str.replace(/<br>/g,    "\n ");
		form_obj.content.value = this.comment_trim(conent_str).replace(/<.*?>/g,"");

		if(this.comment_trim(form_obj.content.value).length >1000) {
			$(form_id+'_msg').innerHTML = '<div class="warning-tip tip-box warning-tip-box"><div><span class="fixie6png tip-icon warning-tip-icon" ></span>超出字数限制！</div></div>';
			return 0;
		}else  return 1;
	},
	comment_trim:function (str) {
		return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
	},
	remove_state: function(id){
		var request_url = '~comment.signature.delete_state';
		var myAjax = new Ajax.Request(request_url,{
			method : 'post',
			parameters :'quest_id='+id, 
			asynchronous: true,
			onComplete : function (transport){
				//window.location.reload();
				//alert(transport.responseText);
					window.location.href='~comment.signature.index';
					//setInterval(this.refush('~comment.signature.index'),500);;
				 }
		});
	},
	add_comment: function(form_id, editor_id){
		$(form_id).content.value = editor_id.getHtml();
		var check = this.comment_submit_check(form_id); //alert(check);   
		if(!check){ return false;}
		var form_obj=$(form_id);
		var pars=form_obj.serialize();
		form_obj.disable();
		var request_url ='/~comment.index.send_comment';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters : pars,
				asynchronous: true,
				onComplete : function (transport){
					//alert(transport); alert(1);
					var ret = transport.responseText.evalJSON();
					//alert(ret);
					this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id'],ret['data']['uid']);					
				}.bindAsEventListener(this)
			});
		return false;
	},
	add_record:function(form_id){  
		this.record_submit_check(form_id);
		var form_obj = $(form_id);
		var pars = form_obj.serialize(); 
		var request_url = '/~comment.record.add_record';
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters :pars,
				asynchronous: true,
				onComplete : function (){
					window.location.reload();									
				}
			});
	},
	record_submit_check:function(form_id){
		var form_obj = $(form_id);
		if(form_obj.state.value.length>50){
			alert('您输入的内容太长！');
			return false;
		}else if(form_obj.state.value.length <= 0){
			alert('您输入的内容太短！');
			return false;
		}else{
			return true;
		}
	},
	update_record:function(form_id){
		this.record_submit_check(form_id);
		var form_obj = $(form_id);
		var pars = form_obj.serialize();
		var request_url = '/~comment.record.update_record';
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters :pars,
				asynchronous: true,
				onComplete : function (){ //window.location.reload();
					window.location.href = '/~comment.record.index';
				}
			});
	},
	show_reply:function(div_id){
		$(div_id).style.display = 'block';
		window.setTimeout(adjust_content_frame,50);
		
	},
	hidden_reply:function(div_id){
		$(div_id).style.display = 'none';
	},
	comment_smile:function(content){
		//alert(content);
		var regex = '/\[\/s\:([\d]+)\:\/\]/g';
		//var regex = /[a-z](\d)[a-z]/g;
		var content = content.replace(regex, '<img src="/res/comment/images/smiles/em$1.gif">');
		document.write(content);
	},
	add_state_index: function(form_id){	
		var check = this.state_submit_check(form_id);  	//	if($('feel').value.length ==0){  return false;}
		if(check){
		var form_obj=$(form_id);
		var pars=form_obj.serialize();
		var request_url ='/~comment.signature.add_state';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters : pars,
				onComplete : function (transport){
					 window.location.reload();
					// $('state_index').style.display = 'block';
					//window.location.href='~comment.signature.index';
					//setInterval(this.refush('~comment.signature.index'),500);
				}.bindAsEventListener(this)
			});
		$(form_id).enable;
		return false;
		}
	},
	change_state_div:function(id){
		$(id).style.display = 'none';
		$('state_new').style.display = 'block';
	},
	show_state_smile:function(id){
		$(id).style.dispaly = 'block';
	},
	state_face:function(id,face){

		var img_url = '/res/comment/images/smiles2/em'+id+'.gif';
		//alert(img_url);
		$('state_button').style.background = "url("+img_url+") no-repeat";
		$('state_button').style.height = '19px';
		if(id==1){  $('smile_tage').value=''; $('state_button').style.background="url() no-repeat";}
		else {  $('smile_tage').value=face; }
		//$('state_smile').style.dispaly = 'none';
	},
	MaxTextareaLen : 100,
	last : 100,
	TextCountLen : new Array(),
	countTextShow : function (textareaName,spanName) {	//alert(textareaName);	//显示文本区域字数函数
		if($(textareaName).value.length <= this.MaxTextareaLen){
			this.last = this.MaxTextareaLen - $(textareaName).value.length;
			this.TextCountLen[this.MaxTextareaLen] = $(textareaName).value;
			//$(spanName).innerHTML=" 已输入" + $(textareaName).value.length +"个字；剩余" + this.last + "个字。";
			$(spanName).innerHTML=$(textareaName).value.length+'/100';
		}else{
			this.last = 0;
			this.TextCountLen[this.MaxTextareaLen] = $(textareaName).value.substr(0,this.MaxTextareaLen);
			$(textareaName).value = this.TextCountLen[this.MaxTextareaLen];
			$(textareaName).focus();
		}
	},
	change_index_div:function(div_id,div_id2){
		$(div_id).style.display = 'none';
		$(div_id2).style.display = 'block';
	},
	breakWord:function(str,len){
		var reg = new RegExp();
		reg  = /^[\d|a-zA-Z]+$/; 
		if(reg.test(str)){			
			if(str.length>len){
				var strTemp="";    
				while(str.length>len){    
					strTemp+=str.substr(0,len)+"&#10;";     
					str=str.substr(len,str.length);     
				}    
				strTemp+="&#10;"+str;  
				str = strTemp;
			}
		}
		document.write(str);
	},
	keypress:function (editor_id,time_str){
		return function(){
			var string = editor_id.getHtml();		//	alert(string);
				string.length = string.length + 2 ;
			if(string.length <= 50){
//					this.last = this.MaxTextareaLen - str.length;
//					this.TextCountLen[this.MaxTextareaLen] = str;
					//$(spanName).innerHTML=" 已输入" + $(textareaName).value.length +"个字；剩余" + this.last + "个字。";
					$('add_comment_'+time_str+'_msg').innerHTML=string.length+'/100';
				}else{
					this.last = 0; 
					var string_sub = string.substr(0,50);
					editor_id.innerhtml = string_sub;
//					$(textareaName).focus();
				}
			
			
		}
	}
//		,
//	ctlent:function(event,id){
//		if((event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83) ) { alert(2);		return 1;
//		}
//		//var cText = $('content_'+form_id);this.form_submit = action+'_'+form_id; alert(this.form_submit);  this.action = action;
//		//cText.onkeypress = this.SubmitOnce.bindAsEventListener(this);
//		
//	},
//	SubmitOnce : function(evt) {
//        var x = evt.keyCode; 
//        var q = evt.ctrlKey;
//        if (q && ( x == 13 || x == 10)) {
//        	//var action2 = this.action; alert(eval(this.action2));
//        	$(this.form_submit).submit();
//        	 Form.disable(Form.getElements(this.form_submit));
//           // this.action2(this.form_submit);
//            //Form.disable(this.cForm);
//        }
//    }
});

var smiles = Class.create();
Object.extend(smiles, {
	hide:false,
	show_smile:function(id){
			$(id).blur();
			if(smiles.hide){
				smiles.hide = false;
				$(id).style.display = 'none';

			}else{
				smiles.hide = true;
				$(id).style.display = 'block';
				$(id).focus();

			}
		},
	chick_face:function(id,tag){
		var connent = $('content_'+id).value;
		    connent = connent+tag;
		    $('content_'+id).value = connent;
			$('smile_'+id).style.display = 'none';
	},
	hide_smile:function(id){ //alert(id);
			//smiles.hide = false;
			$(id).style.display = 'none';
		//}

	},
	show_smile_index:function(event,id){
			event = $E(event);
			var src = Event.element(event);
			var dem = src.getDimensions();
			var offset = src.cumulativeOffset();
			var t = new Template(Gift.template);
			$('smile').absolutize();
			$('smile').setStyle({
				top: dem.height - 20 + 'px',
				left: 50 + 'px'
			});
		//	$(id).style.display='none';
	}

});


